<?php

namespace app\index\controller;

use tian\Session;

class Pets extends Base
{
    public function index()
    {
        try {
            $role_id = Session::get('uid');

            $where = [];
            $where[] = ['uid', '=', $role_id];
            $list = model('RolePets')->getList($where);
            if(!empty($list)){
                foreach ($list as $k=>$item){
                    $item['str'] = str_base64_encode($item['cwid'],$role_id);
                }
            }
            $this->assign('list', $list);

            $this->fetch('index');
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    public function info()
    {
        try {
            $role_id = Session::get('uid');

            $str = input('str');
            $this->assign('str', $str);

            $target_id = $this->strAnalysis($str, $role_id);

            $infoRolePets = model('RolePets')->getInfo($target_id);
            $this->assign('infoRolePets', $infoRolePets);

            $this->fetch('info');
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }
    public function petsAct()
    {
        try {
            $request = request();
            if ($request->method() == 'POST') {
                $role_id = Session::get('uid');

                $type = input('type');
                $str = input('str');
                $target_id = $this->strAnalysis($str, $role_id);

                $infoRole = model('Role')->getInfo($role_id);
                $infoRolePets = model('RolePets')->getInfo($target_id);

                if($type=='gowar'){
                    $tempRole = [
                        'uid' => $role_id,
                        'uhp' => $infoRole['uhp'] + $infoRolePets['uphp'],
                        'umaxhp' => $infoRole['umaxhp'] + $infoRolePets['uphp'],
                        'ugj' => $infoRole['ugj'] + $infoRolePets['upgj'],
                        'ufy' => $infoRole['ufy'] + $infoRolePets['upfy'],
                        'cw' => $target_id,
                    ];
                    model('Role')->saved($tempRole);
                }
                if($type=='rest'){
                    $tempRole = [
                        'uid' => $role_id,
                        'uhp' => $infoRole['uhp'] - $infoRolePets['uphp'],
                        'umaxhp' => $infoRole['umaxhp'] - $infoRolePets['uphp'],
                        'ugj' => $infoRole['ugj'] - $infoRolePets['upgj'],
                        'ufy' => $infoRole['ufy'] - $infoRolePets['upfy'],
                        'cw' => 0,
                    ];
                    model('Role')->saved($tempRole);
                }

                returnJson(1, 'ok');
            } else {
                returnJson(0, '非法请求');
            }
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }
}
